From 408f4e314553b3c8493c5c9e6bb1b72b336161cc Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Tue, 16 May 2017 08:11:23 -0700 Subject: [PATCH] Only sort deps in build script dependency generation --- src/cargo/core/resolver/mod.rs | 6 ------ src/cargo/ops/cargo_rustc/context.rs | 7 +++--- src/cargo/ops/cargo_rustc/custom_build.rs | 10 ++++++++- src/cargo/ops/cargo_rustc/mod.rs | 3 +++ tests/build-script.rs | 26 ++--------------------- tests/build.rs | 4 ++-- 6 files changed, 19 insertions(+), 37 deletions(-) diff --git a/src/cargo/core/resolver/mod.rs b/src/cargo/core/resolver/mod.rs index 1a3e217bf..896b28046 100644 --- a/src/cargo/core/resolver/mod.rs +++ b/src/cargo/core/resolver/mod.rs @@ -200,12 +200,6 @@ unable to verify that `{0}` is the same as when the lockfile was generated Deps { edges: self.graph.edges(pkg), resolve: self } } - pub fn deps_sorted(&self, pkg: &PackageId) -> Vec<&PackageId> { - let mut deps = self.deps(pkg).collect::>(); - deps.sort(); - deps - } - pub fn deps_not_replaced(&self, pkg: &PackageId) -> DepsNotReplaced { DepsNotReplaced { edges: self.graph.edges(pkg) } } diff --git a/src/cargo/ops/cargo_rustc/context.rs b/src/cargo/ops/cargo_rustc/context.rs index 49565ad9f..e17a0823b 100644 --- a/src/cargo/ops/cargo_rustc/context.rs +++ b/src/cargo/ops/cargo_rustc/context.rs @@ -614,8 +614,8 @@ impl<'a, 'cfg> Context<'a, 'cfg> { } let id = unit.pkg.package_id(); - let deps = self.resolve.deps_sorted(id); - let mut ret = deps.iter().filter(|dep| { + let deps = self.resolve.deps(id); + let mut ret = deps.filter(|dep| { unit.pkg.dependencies().iter().filter(|d| { d.name() == dep.name() && d.version_req().matches(dep.version()) }).any(|d| { @@ -748,8 +748,7 @@ impl<'a, 'cfg> Context<'a, 'cfg> { /// Returns the dependencies necessary to document a package fn doc_deps(&self, unit: &Unit<'a>) -> CargoResult>> { - let deps = self.resolve.deps_sorted(unit.pkg.package_id()); - let deps = deps.iter().filter(|dep| { + let deps = self.resolve.deps(unit.pkg.package_id()).filter(|dep| { unit.pkg.dependencies().iter().filter(|d| { d.name() == dep.name() }).any(|dep| { diff --git a/src/cargo/ops/cargo_rustc/custom_build.rs b/src/cargo/ops/cargo_rustc/custom_build.rs index 64f05f918..930662151 100644 --- a/src/cargo/ops/cargo_rustc/custom_build.rs +++ b/src/cargo/ops/cargo_rustc/custom_build.rs @@ -464,7 +464,15 @@ pub fn build_map<'b, 'cfg>(cx: &mut Context<'b, 'cfg>, if !unit.target.is_custom_build() && unit.pkg.has_custom_build() { add_to_link(&mut ret, unit.pkg.package_id(), unit.kind); } - for unit in cx.dep_targets(unit)?.iter() { + + // We want to invoke the compiler deterministically to be cache-friendly + // to rustc invocation caching schemes, so be sure to generate the same + // set of build script dependency orderings via sorting the targets that + // come out of the `Context`. + let mut targets = cx.dep_targets(unit)?; + targets.sort_by_key(|u| u.pkg.package_id()); + + for unit in targets.iter() { let dep_scripts = build(out, cx, unit)?; if unit.target.for_host() { diff --git a/src/cargo/ops/cargo_rustc/mod.rs b/src/cargo/ops/cargo_rustc/mod.rs index 40c53731f..c82788794 100644 --- a/src/cargo/ops/cargo_rustc/mod.rs +++ b/src/cargo/ops/cargo_rustc/mod.rs @@ -770,6 +770,9 @@ fn build_base_args(cx: &mut Context, cmd.arg("--cfg").arg("test"); } + // We ideally want deterministic invocations of rustc to ensure that + // rustc-caching strategies like sccache are able to cache more, so sort the + // feature list here. for feat in cx.resolve.features_sorted(unit.pkg.package_id()) { cmd.arg("--cfg").arg(&format!("feature=\"{}\"", feat)); } diff --git a/tests/build-script.rs b/tests/build-script.rs index da2a45649..d07eefe69 100644 --- a/tests/build-script.rs +++ b/tests/build-script.rs @@ -2678,30 +2678,8 @@ fn deterministic_rustc_dependency_flags() { assert_that(p.cargo_process("build").arg("-v"), execs().with_status(0) - .with_stderr("\ -[UPDATING] [..] -[DOWNLOADING] [..] -[DOWNLOADING] [..] -[DOWNLOADING] [..] -[DOWNLOADING] [..] -[COMPILING] [..] -[COMPILING] [..] -[COMPILING] [..] -[COMPILING] [..] -[RUNNING] [..] -[RUNNING] [..] -[RUNNING] [..] -[RUNNING] [..] -[RUNNING] [..] -[RUNNING] [..] -[RUNNING] [..] -[RUNNING] [..] -[RUNNING] [..] -[RUNNING] [..] -[RUNNING] [..] -[RUNNING] [..] -[COMPILING] foo v0.1.0 [..] + .with_stderr_contains("\ [RUNNING] `rustc --crate-name foo [..] -L native=test1 -L native=test2 \ -L native=test3 -L native=test4` -[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]")); +")); } diff --git a/tests/build.rs b/tests/build.rs index e4cbfff20..fc4d28983 100644 --- a/tests/build.rs +++ b/tests/build.rs @@ -3182,8 +3182,8 @@ fn deterministic_cfg_flags() { [RUNNING] [..] [RUNNING] [..] [RUNNING] `rustc --crate-name foo [..] \ ---cfg 'feature=\"default\"' --cfg 'feature=\"f_a\"' --cfg 'feature=\"f_b\"' \ ---cfg 'feature=\"f_c\"' --cfg 'feature=\"f_d\"' [..] \ +--cfg[..]default[..]--cfg[..]f_a[..]--cfg[..]f_b[..]\ +--cfg[..]f_c[..]--cfg[..]f_d[..] \ --cfg cfg_a --cfg cfg_b --cfg cfg_c --cfg cfg_d --cfg cfg_e` [FINISHED] dev [unoptimized + debuginfo] target(s) in [..]")); } -- 2.30.2